package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.ReportContractResultAddBo;
import com.xjy.system.domain.bo.ReportContractResultQueryBo;
import com.xjy.system.domain.bo.ReportContractResultEditBo;
import com.xjy.system.domain.ReportContractResult;
import com.xjy.system.mapper.ReportContractResultMapper;
import com.xjy.system.domain.vo.ReportContractResultVo;
import com.xjy.system.service.IReportContractResultService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * VIEWService业务层处理
 *
 * @author xjy
 * @date 2022-04-03
 */
@Service
public class ReportContractResultServiceImpl extends ServicePlusImpl<ReportContractResultMapper, ReportContractResult> implements IReportContractResultService {

    @Override
    public ReportContractResultVo queryById(Long id){
        return getVoById(id, ReportContractResultVo.class);
    }

    @Override
    public TableDataInfo<ReportContractResultVo> queryPageList(ReportContractResultQueryBo bo) {
        PagePlus<ReportContractResult, ReportContractResultVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ReportContractResultVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ReportContractResultVo> queryList(ReportContractResultQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ReportContractResultVo.class);
    }

    private LambdaQueryWrapper<ReportContractResult> buildQueryWrapper(ReportContractResultQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportContractResult> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getProjName()), ReportContractResult::getProjName, bo.getProjName());
        lqw.eq(StrUtil.isNotBlank(bo.getConstractNo()), ReportContractResult::getConstractNo, bo.getConstractNo());
        lqw.like(StrUtil.isNotBlank(bo.getConstractName()), ReportContractResult::getConstractName, bo.getConstractName());
        lqw.like(StrUtil.isNotBlank(bo.getNailName()), ReportContractResult::getNailName, bo.getNailName());
        lqw.like(StrUtil.isNotBlank(bo.getBname()), ReportContractResult::getBname, bo.getBname());
        lqw.eq(StrUtil.isNotBlank(bo.getConstractBigType()), ReportContractResult::getConstractBigType, bo.getConstractBigType());
        lqw.eq(StrUtil.isNotBlank(bo.getConstractSmallType()), ReportContractResult::getConstractSmallType, bo.getConstractSmallType());
        lqw.eq(bo.getCloseAmount() != null, ReportContractResult::getCloseAmount, bo.getCloseAmount());
        lqw.eq(bo.getSignTime() != null, ReportContractResult::getSignTime, bo.getSignTime());
        lqw.eq(StrUtil.isNotBlank(bo.getContent()), ReportContractResult::getContent, bo.getContent());
        lqw.eq(StrUtil.isNotBlank(bo.getContactsor()), ReportContractResult::getContactsor, bo.getContactsor());
        lqw.eq(StrUtil.isNotBlank(bo.getContactsPhone()), ReportContractResult::getContactsPhone, bo.getContactsPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getContactsEmai()), ReportContractResult::getContactsEmai, bo.getContactsEmai());
        lqw.eq(StrUtil.isNotBlank(bo.getContactsOthers()), ReportContractResult::getContactsOthers, bo.getContactsOthers());
        lqw.like(StrUtil.isNotBlank(bo.getProjResultName()), ReportContractResult::getProjResultName, bo.getProjResultName());
        lqw.eq(bo.getProjAmt() != null, ReportContractResult::getProjAmt, bo.getProjAmt());
        lqw.eq(StrUtil.isNotBlank(bo.getColpayCycle()), ReportContractResult::getColpayCycle, bo.getColpayCycle());
        lqw.eq(StrUtil.isNotBlank(bo.getColpayCondition()), ReportContractResult::getColpayCondition, bo.getColpayCondition());
        lqw.eq(bo.getColpayAmt() != null, ReportContractResult::getColpayAmt, bo.getColpayAmt());
        lqw.eq(bo.getColpayExpireTime() != null, ReportContractResult::getColpayExpireTime, bo.getColpayExpireTime());
        lqw.like(StrUtil.isNotBlank(bo.getAccountName()), ReportContractResult::getAccountName, bo.getAccountName());
        lqw.eq(StrUtil.isNotBlank(bo.getOpenBank()), ReportContractResult::getOpenBank, bo.getOpenBank());
        lqw.eq(StrUtil.isNotBlank(bo.getAccount()), ReportContractResult::getAccount, bo.getAccount());
        lqw.like(StrUtil.isNotBlank(bo.getColBankName()), ReportContractResult::getColBankName, bo.getColBankName());
        lqw.eq(StrUtil.isNotBlank(bo.getColBank()), ReportContractResult::getColBank, bo.getColBank());
        lqw.eq(StrUtil.isNotBlank(bo.getColBankNo()), ReportContractResult::getColBankNo, bo.getColBankNo());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ReportContractResultAddBo bo) {
        ReportContractResult add = BeanUtil.toBean(bo, ReportContractResult.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ReportContractResultEditBo bo) {
        ReportContractResult update = BeanUtil.toBean(bo, ReportContractResult.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ReportContractResult entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
